//
//  ST_BTUserInfoManager+request.swift
//  btvideo
//
//  Created by Jason on 2021/11/2.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import Foundation
import RxCocoa
import RxSwift
import NSObject_Rx
import SwiftyUserDefaults
import SwiftyJSON

extension ST_BTUserInfoManager{
    
     /// 重试延时 1 秒
    private static let retry_delay: RxTimeInterval = RxTimeInterval.seconds(1)
    
    ///封禁状态查询
    func query_forbid_status(){
        
        JVrequest(PH.shumeiFetchUserStatusApi) { (json) in
            
            self.additional_attributes?.SMForbidden = json["data"]["shumei_forbid"].boolValue
            
        } failure: { (code, err) in
            
            
            print("shumei_forbid error:",err)
        }
    }
    
    /// 更新每日初体验数据
    func update_additional_impression_data(json:JSON){
        
        main_thread_execute {
            
            self.additional_attributes?.updateImpressionData(json: json)
            
            self.user_additional_attributes_update.accept(())
            
        }
        
    }
    
    
    /// 拉取用户附加信息/个性化配置
    func pull_additional_attributes(){
        
        
        if Defaults[.BTLoginSuccess] == false{
            return
        }
        
        guard let user = ST_BTUserInfoManager.shared.current_account_info, user.token.count > 1 else{
            
            return
        }
        Observable.zip(TIANSession(requestUrl: PH.fetchUserOtherInfoApi).wrap_response_data().retry { error -> Observable<Int> in
            return Observable.timer(ST_BTUserInfoManager.retry_delay, scheduler: MainScheduler.instance)
        }, TIANSession(requestUrl: PH.fetchUserBaseDataApi).wrap_response_data().retry { error -> Observable<Int> in
            return Observable.timer(ST_BTUserInfoManager.retry_delay, scheduler: MainScheduler.instance)
        }).subscribe(onNext: { json0,json1 in
            
            let data = json1["data"]
            
            
            print("#bug1065 - voice_sign_status:",data["voice_sign_status"].intValue)
            
            self.additional_attributes = ST_BTAccountAdditionalAttribute(data: data, roomConfigData: json0["data"])
            
            print("#bug1065 additional_attributes.voiceSignStatus: ",self.additional_attributes?.voiceSignStatus ?? "nil")
            
            
            self.query_forbid_status()
            
            main_thread_execute {
                ST_BTUserInfoManager.shared.modify_curr_base_info(json: data)
                
                self.user_additional_attributes_update.accept(())
            }
            
            SCComponets.delaytask.runScript(afterDelay: data["auto_send_msg_time"].doubleValue)
            
        }).disposed(by: rx.disposeBag)
        
    }
    
}
